/*
 * Copyright ©2011-2016 中国民航信息网络股份有限公司
 */

package com.company;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Solution
{
    public static Object eval(String exp){
        Pattern pattern = Pattern.compile("^(|-)\\d+$");
        Matcher matcher = pattern.matcher(exp);
        if(matcher.find(0)){
            return Integer.valueOf(exp);
        }

        pattern = Pattern.compile("^((|-)\\d+)([-+*/%])((|-)\\d+)$");
        matcher = pattern.matcher(exp);

        if(matcher.find(0)){
            String operator = matcher.group(3);
            int num1 = Integer.valueOf(matcher.group(1));
            int num2 = Integer.valueOf(matcher.group(4));
            switch (operator){
                case "+": return num1 + num2;
                case "-": return num1 - num2;
                case "*": return num1 * num2;
                case "/": return num1 / num2;
                case "%": return num1 % num2;
            }
        }
        return 0;
    }

    public static Object constriction(String exp){
        System.out.println(exp);
        // a+b或a
        Pattern pattern = Pattern.compile("^(((|-)\\d+[-+*/%](|-)\\d+)|((|-)\\d+))$");
        Matcher matcher = pattern.matcher(exp);

        if(matcher.find(0)){
            return eval(matcher.group());
        }

        //去()
        pattern = Pattern.compile("(.*)\\((.*?)\\)(.*)");
        matcher = pattern.matcher(exp);

        if(matcher.find(0)){
            exp = matcher.group(1) + constriction(matcher.group(2)) + matcher.group(3);
            return constriction(exp);
        }

        //先算乘除
        pattern = Pattern.compile("(|.*?[-+])((|-)\\d+[*/%](|-)\\d+)(.*)");
        matcher = pattern.matcher(exp);

        if(matcher.find(0)){
            exp = matcher.group(1) + constriction(matcher.group(2)) + matcher.group(5);
            return constriction(exp);
        }

        //再算加减
        pattern = Pattern.compile("((|-)\\d+[-+](|-)\\d+)(.*)");
        matcher = pattern.matcher(exp);

        if(matcher.find(0)){
            exp = constriction(matcher.group(1)) + matcher.group(4);
            return constriction(exp);
        }

        return 0;
    }

    public static String preproccess(String exp){
        exp = exp.replaceAll(" ", "")
                .replaceAll("\\[", "\\(")
                .replaceAll("\\]", "\\)")
                .replaceAll("\\(((|-)\\d+)\\)", "$1");

        return exp;
    }


    public static void main(String [] args) {
        String exp = preproccess(args[0]);

        System.out.println(constriction(exp));
    }
}